package com.yhsoft.benefitcard.service.impl;

import com.yhsoft.benefitcard.controller.response.CardsInfoOutVo;
import com.yhsoft.benefitcard.mapper.ReportMapper;
import com.yhsoft.benefitcard.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Service
@Slf4j
public class ReportServiceImpl  implements ReportService {
    @Resource
    private ReportMapper reportMapper;

    @Override
    public void exportBusinessData(String firstBatchNum, HttpServletResponse response) throws IOException {
        try {
            List<CardsInfoOutVo> logData = reportMapper.select(CardsInfoOutVo.class, firstBatchNum);
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("福利卡报表");
            //创立标题行
            XSSFRow headerRow = sheet.createRow(0);
            String[] headers = {"激活批次", "卡号", "卡密"};
            for (int i = 0; i < headers.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headers[i]);
            }
            for (int i = 0; i < logData.size(); i++) {
                CardsInfoOutVo cardsInfoOutVo = logData.get(i);
                Row row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(cardsInfoOutVo.getSecondBantchNum());
                row.createCell(1).setCellValue(cardsInfoOutVo.getBenefitCardsNum());
                row.createCell(2).setCellValue(cardsInfoOutVo.getBenefitCardsPassword());
            }
            for (int i = 0; i < headers.length; i++) {
                sheet.autoSizeColumn(i);
            }
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("福利卡列表", "UTF-8") + ".xlsx");
            //保存文件
            //通过输出流将excel文件下载到客户端浏览器
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);

            //关闭资源
            out.close();
            workbook.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("上传成功！");
    }

    @Override
    public void exportBusinessData2(String batch2Num, HttpServletResponse response) {
        try {
            List<CardsInfoOutVo> logData = reportMapper.select2(CardsInfoOutVo.class, batch2Num);
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("福利卡报表");
            //创立标题行
            XSSFRow headerRow = sheet.createRow(0);
            String[] headers = {"激活批次", "卡号", "卡密"};
            for (int i = 0; i < headers.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headers[i]);
            }
            for (int i = 0; i < logData.size(); i++) {
                CardsInfoOutVo cardsInfoOutVo = logData.get(i);
                Row row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(cardsInfoOutVo.getSecondBantchNum());
                row.createCell(1).setCellValue(cardsInfoOutVo.getBenefitCardsNum());
                row.createCell(2).setCellValue(cardsInfoOutVo.getBenefitCardsPassword());
            }
            for (int i = 0; i < headers.length; i++) {
                sheet.autoSizeColumn(i);
            }
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("福利卡列表", "UTF-8") + ".xlsx");
            //保存文件
            //通过输出流将excel文件下载到客户端浏览器
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);

            //关闭资源
            out.close();
            workbook.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("上传成功！");
    }
}
